import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;




public class Canvas extends JPanel {

	  //Store the list of elements
	  public Vector<DraggableImageComponent> displayList = new Vector<DraggableImageComponent>();
	  
	  //Store the list of labels
	  public Vector<MyLabel> labelList = new Vector<MyLabel>();
	  
	  //Number of buttons present
	  private int numB = 0;
	  private int bSpacing = 0;
	  
	  private String word = "-----";
	  
	  private boolean showGrid = true;
	
	 
	  /**
	   * Initiate the Canvas object
	   * 
	   */
	  public void init(){
		  
		setBackground(new Color(255,255,255));
		setLayout(null);
		setPreferredSize(new Dimension(702,498));
	  }
	  
	  /**
	   * Add Element to the list of display
	   * 
	   * @param e new Element
	   */
	  public void addElement(Image img){
		  
		    DraggableImageComponent photo = new DraggableImageComponent();
	        add(photo);
	        displayList.add(photo);
	        photo.setImage(img);
	        photo.setAutoSize(true);
	        photo.setOverbearing(false);
	        photo.setLocation(0,0);
	        photo.setSize(200,200);
	        
	        repaint();
		  
	  }
	  
	  /**
	   * Add Label to the list of display
	   * 
	   * @param txt label text
	   */
	  public void addLabel(String txt){
		  
		    MyLabel label = new MyLabel(txt);
	        add(label);
	        labelList.add(label);
	        
	        repaint();
		  
	  }
	  
	  public void resetButton(){
		  numB = 0;
	  }
	  
	  public void resetWord(){
		  word = "-----";
	  }
	  
	  
	  public void drawButtons(int numB, int spacing){
		this.numB = numB;
		this.bSpacing = spacing;
		
		repaint();
	  }
	  
	  public void drawCards(String word){
			this.word = word;
			
			repaint();
		  }
	  
	  public Vector<DraggableImageComponent> getElements(){
		  return displayList;
	  }
	  
	  public Vector<MyLabel> getLabels(){
		  return labelList;
	  }
	  
	  public void setGrid(boolean showgrid){
		  this.showGrid = showgrid;
	  }
	  
	  /**
	   * Override paintComponent
	   * 
	   * @param g the Graphics2D object
	   */
	  @Override
	  protected void paintComponent(Graphics g)
	  {
	    Graphics2D g2 = (Graphics2D) g;
	    
	    //Set anti-alising
	    RenderingHints rh = g2.getRenderingHints ();
	    rh.put (RenderingHints.KEY_ANTIALIASING,
	            RenderingHints.VALUE_ANTIALIAS_ON);
	    g2.setRenderingHints (rh);
	    
	    super.paintComponent(g); 
	    
	    
	  //Paint the background
	    if(showGrid)
	    	paintBackground(g2);
	    
	    //Paint buttons
	    paintButtons(g2);
	    
	    //Paint cards
	    paintCards(g2);
	    
	    
	  }
	  
	  /**
	   * Draw the buttons
	   * 
	   * @param g the Graphics2D object
	   */
	  private void paintButtons(Graphics2D g){
		if(numB > 0)
		{
			int margin = 100;
		    g.setColor(Color.black);
		    g.setStroke(new BasicStroke(15));
		    
		    for(int i=1; i<=numB; i++){
		        g.drawLine(bSpacing*i,this.getHeight() - margin - 200,bSpacing*i,this.getHeight() - margin);
		        
		        //Draw buttons
		        try {
		        	Image img = ImageIO.read(getClass().getResource("resources/s"+i+".png"));
					Image newimg = img.getScaledInstance(60, 60,  java.awt.Image.SCALE_SMOOTH ) ;  
					g.drawImage(newimg,bSpacing*i - 30, this.getHeight() - margin - 200 -30,null);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        
		    }
		    
		    g.setColor(Color.black);
		    g.drawLine(bSpacing,this.getHeight() - margin,this.getWidth(),this.getHeight() - margin);
		}
	  }
	  
	  /**
	   * Draw grids
	   * 
	   * @param g the Graphics2D object
	   */
	  private void paintBackground(Graphics2D g){
		  g.setColor(new Color(233,233,233));
		  int step = 15;
		    
		    for(int i=step; i<getWidth(); i=i+step){
		        g.drawLine(i,0,i,getHeight());
		    }
		    
		    for(int i=step; i<getHeight(); i=i+step){
		        g.drawLine(0,i,getWidth(),i);
		    }
	  }
	  
	  /**
	   * Draw the cards
	   * 
	   * @param g the Graphics2D object
	   */
	  private void paintCards(Graphics2D g){
		if(!word.equals("-----"))
		{
			Font f = new Font("Dialog", Font.BOLD, 70);
			g.setFont(f);
			FontMetrics fm = g.getFontMetrics();
			g.setColor(Color.white);
		    g.setStroke(new BasicStroke(20));
			int margin = 100;
		    int space = getWidth() / (7 + 1);
		    
		    for(int i=1; i<=word.length(); i++){
		        
		        //Draw front
		        try {
		        	Image img = ImageIO.read(getClass().getResource("resources/background.png"));
		        	Image newimg = img.getScaledInstance(77,119,  java.awt.Image.SCALE_SMOOTH ) ;  
					g.drawImage(newimg,i*space-(77/2), margin,null);
		        	g.drawString(""+word.charAt(i-1), i*space-(fm.charWidth(word.charAt(i-1))/2), margin + 119/2 + fm.getHeight()/4);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        
		        //Draw back
		        try {
		        	Image img = ImageIO.read(getClass().getResource("resources/card"+i+".png"));
					Image newimg = img.getScaledInstance(77, 119,  java.awt.Image.SCALE_SMOOTH ) ;  
					g.drawImage(newimg,i*space-(77/2), getHeight()-margin - 119,null);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    }
		}
	  }
	  
	 
	  
}
